package com.cy.metrics;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.cy.util.U;
import org.testng.annotations.Test;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * https://metrics.dropwizard.io/3.1.0/
 *
 * @author yue.cao
 * @since 12-29-2020
 */
public class TimerDemo {


	@Test(description = "Meter 单位时间内有多少个事件进来    Timer 一个事件执行耗费了多少时间")
	public void tesTimer() throws InterruptedException {
		MetricRegistry registry = new MetricRegistry();

		ConsoleReporter reporter = ConsoleReporter
				.forRegistry(registry)
				.convertRatesTo(TimeUnit.SECONDS)
				.convertDurationsTo(TimeUnit.SECONDS).build();
		reporter.start(5, TimeUnit.SECONDS);
		Timer timer = registry.timer("req", Timer::new);

		while (true) {
			Timer.Context context = timer.time();
			TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(5));
			context.stop();
		}
	}


}
